package test;

import java.util.ArrayList;
import java.util.Scanner;

class Student {
    String name;
    String id;
    String address;

    public Student() {

    }

    public Student(String name, String id, String address) {
        this.name = name;
        this.id = id;
        this.address = address;
    }

    public String getId() {

        return id;
    }

    public void setId(String id) {

        this.id = id;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {

        this.name = name;
    }

    public String getAddress() {

        return address;
    }

    public void setAddress(String address) {

        this.address = address;
    }

}


public class test {
    public static void main(String[] args) {
        ArrayList<Student> array = new ArrayList<Student>();//创建集合对象，用来存储学生信息
        while (true) {
            System.out.println("----------kinroy学生信息管理系统----------");
            System.out.println("1:添加学生信息");
            System.out.println("2:删除学生信息");
            System.out.println("3:修改学生信息");
            System.out.println("4:查询学生信息");
            System.out.println("5:退出系统");
            System.out.println("请输入你要操作的序号");
            Scanner scan = new Scanner(System.in);
            int vaule = scan.nextInt();
            switch (vaule) {
                case 1:
                    addStudent(array);//添加学生
                    break;
                case 2:
                    deleteStudent(array);//删除学生
                    break;
                case 3:
                    dateStudent(array);//修改学生
                    break;
                case 4:
                    findStudent(array);//查询学生
                    break;
                case 5:
                    System.out.println("谢谢使用！");
                    System.exit(0);
            }
        }
    }

    //添加学生
    public static void addStudent(ArrayList<Student> array) {
        //创建对象，接收学生信息
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入学生学号:");
        String id = scan.next();
        System.out.println();
        System.out.println("请输入学生姓名:");
        String name = scan.next();
        System.out.println();
        System.out.println("请输入学生地址");
        String address = scan.next();

        //创建学生对象
        Student st = new Student();
        st.setId(id);
        st.setName(name);
        st.setAddress(address);

        array.add(st);//将学生信息添加到数组中
        System.out.println("添加学生成功！");
    }

    //查找学生
    public static void findStudent(ArrayList<Student> array) {
        System.out.println("查询单个学生扣1,查询所有学生扣2");
        Scanner scan = new Scanner(System.in);
        int vaule = scan.nextInt();
        if (vaule == 1) {
            System.out.print("请输入你要查询学生信息的id:");
            String id = scan.next();

            int index = -1;
            for (int i = 0; i < array.size(); i++) {
                Student st = array.get(i);
                if (st.getId().equals(id)) {//匹配学生id
                    Student st1 = array.get(i);//得到学生信息
                    System.out.println(st.getName() + "\t\t" + st.getId() + "\t\t" + st.getAddress());
                    break;
                }
            }
        } else if (vaule == 2){
            if (array.size() == 0) {
                System.out.println("没有该学生信息，请添加");
                return;
            }
            System.out.println("姓名\t\t学号\t\t地址\t\t");//表头信息
            for (int i = 0; i < array.size(); i++) {
                Student st2 = array.get(i);//得到学生信息
                System.out.println(st2.getName() + "\t\t" + st2.getId() + "\t\t" + st2.getAddress());
            }
        }
    }
    //删除学生
    public static void deleteStudent(ArrayList<Student> array) {
        System.out.println("请输入你要删除学生的id:");
        Scanner scan = new Scanner(System.in);
        String id = scan.next();

        //遍历数组，判断id是否存在
        int index = -1;
        for (int i = 0; i < array.size(); i++) {
            Student st = array.get(i);
            if (st.getId().equals(id)) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("该学生信息不存在");
        } else {
            array.remove(index);
            System.out.println("删除成功");
        }
    }

    //修改学生
    public static void dateStudent(ArrayList<Student> array) {
        System.out.println("请输入你要修改学生的id:");
        Scanner scan = new Scanner(System.in);
        String id = scan.next();
        System.out.println("请输入新的姓名:");
        String name = scan.next();
        System.out.println("请输入新的地址");
        String address = scan.next();

        //创建学生对象
        Student st = new Student();
        st.setId(id);
        st.setName(name);
        st.setAddress(address);

        int index = -1;
        for (int i = 0; i < array.size(); i++) {
            Student student = array.get(i);
            if (st.getId().equals(id)) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("该学生信息不存在");
        } else {
            array.set(index, st);
            System.out.println("修改成功");
        }
    }
}